#include <stdio.h>
#include <stdbool.h>

typedef struct date
{
    int year;
    int month;
    int day;
} Date;

enum MON
{
    JAN,
    FEB,
    MAR,
    APR,
    MAY,
    JUN,
    AUG,
    SEP,
    OCT,
    NOV,
    DEC
};
const char *Mon[] = {"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "AUG", "SEP", "OCT", "NOV", "DEC"};

void printDate(Date *d[], int n)
{
    for (int i = 0; i < n; ++i)
    {
        printf("%s-%02d-%04d ", Mon[d[i]->month - 1], d[i]->day, d[i]->year);
        printf("\n");
    }
}

bool isLeapYear(int year)
{
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
    {
        return true;
    }
    return false;
}

int checkisDate(Date *d)
{
    if (d->month < 1 || d->month > 12)
        return 0;
    if (d->day < 1)
        return 0;

    switch (d->month)
    {
    case 4:
    case 6:
    case 9:
    case 11:
        if (d->day > 30)
            return 0;
        break;
    case 2:
        if (isLeapYear(d->year) && d->day > 29)
            return 0;
        if (!isLeapYear(d->year) && d->day > 28)
            return 0;
        break;
    default:
        if (d->day > 31)
            return 0;
    }
    return 1;
}

int compareDate(Date d1, Date d2)
{
    if (d1.year > d2.year)
        return 1;
    if (d1.year < d2.year)
        return -1;

    if (d1.month > d2.month)
        return 1;
    if (d1.month < d2.month)
        return -1;

    if (d1.day > d2.day)
        return 1;
    if (d1.day < d2.day)
        return -1;

    return 0;
}

int main()
{

    Date date[] = {
        {2003, 5, 5},
        {2009, 2, 7}};
    Date *datePtr[2] = {&date[0], &date[1]};

    printDate(datePtr, 2);

    Date date1 = {2007, 2, 29};
    Date date2 = {2020, 2, 29};

    printf("Is date1 vaild? %s\n", checkisDate(&date1) ? "YES" : "NO");
    printf("Is date2 vaild? %s\n", checkisDate(&date2) ? "YES" : "NO");

    Date d1 = {2003, 5, 5};
    Date d2 = {2009, 2, 7};

    int result = compareDate(date1, date2);

    if (result > 0)
    {
        printf("d1 is after d2\n");
    }
    else if (result < 0)
    {
        printf("d1 is before d2\n");
    }
    else
    {
        printf("d1 is the same as d2\n");
    }

    return 0;
}